package com.delicious.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.delicious.dao.BaseDao;
import com.delicious.dao.CategoryDao;
import com.delicious.entity.Category;

public class CategoryDaoImpl extends BaseDao implements CategoryDao {

	public List<Category> getList() {
		String sql = "select * from category";
		openConnection();
		ResultSet rs = null;
		List<Category> list = new ArrayList<Category>();
		
		try {
			rs = executeQuery(sql, null);
			while(rs.next()){
				int id = rs.getInt("id");
				String name = rs.getString("name");
				Category c = new Category(id, name);
				list.add(c);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeConnection();
		}
		
		
		return list;
	}

	public Category getNameByID(int cid) {
		Category c = null;
		String sql = "select * from category where id = ?";
		openConnection();
		ResultSet rs = null ;
		
		try {
			rs = executeQuery(sql, new Object[]{cid});
			if(rs.next()){
				String name = rs.getString("name");
				c = new Category(cid, name);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeConnection();
		}

		return c;
	}

	public static void main(String[] args) {
		Category c = new CategoryDaoImpl().getNameByID(1);
		System.out.println(c.getName());
	}
}
